﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using System.Globalization; 

namespace PSE.Framework.Service.Validation
{
    /// <summary>
    /// Classe para validar intervalo de datas
    /// </summary>
    internal class BvmfDateTimeValidator : Validator<DateTime?>
    {

        /// <summary>
        /// Menor valor do intervalo
        /// </summary>
        private DateTime _lowBoundary;

        /// <summary>
        /// Maior valor do intervalo
        /// </summary>
        private DateTime _highoundary;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="lowBoundary">Menor valor do intervalo</param>
        /// <param name="highBoundary">Maior valor do intervalo</param>
        public BvmfDateTimeValidator(DateTime lowBoundary, DateTime highBoundary) 
            : base(null, null) 
        {
            _lowBoundary = lowBoundary;
            _highoundary = highBoundary; 
        }

        /// <summary>
        /// Método responsável por efetuar o ajuste dos tipos que estão sendo comparados
        /// </summary>
        /// <param name="objectToValidate">Objeto que representa o item a ser validado</param>
        protected override void DoValidate(DateTime? objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate == null)
            {
                validationResults.AddResult(new ValidationResult(this.MessageTemplate, currentTarget, key, this.Tag, this));
            }
            else
            {
                DateTimeRangeValidator validator = new DateTimeRangeValidator(_lowBoundary, RangeBoundaryType.Inclusive, _highoundary, RangeBoundaryType.Inclusive);
                validator.MessageTemplate = this.MessageTemplate;
                validator.Validate((DateTime) objectToValidate, validationResults);
                validator = null;
            }
        }

        /// <summary>
        /// Método de implementação requerida (abstract na classe base) e que retorna DefaultMessageTemplate
        /// </summary>
        protected override string DefaultMessageTemplate
        {
            get { return ""; }
        }
    }
}
